package io.renren.modules.gengyun.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.modules.gengyun.entity.Date_Api;
import io.renren.modules.gengyun.entity.LOTEntity;
import io.renren.modules.gengyun.service.AirConditionHourlyService;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.gengyun.entity.SoilConditionHourlyEntity;
import io.renren.modules.gengyun.service.SoilConditionHourlyService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import javax.validation.constraints.Max;


/**
 * @author
 * @email 2032153705@qq.com
 * @date 2020-09-17 20:31:48
 */
@Api("土壤情况获取接口")
@RestController
@RequestMapping("gengyun/soilconditionhourly")
public class SoilConditionHourlyController {
    @Autowired
    private SoilConditionHourlyService soilConditionHourlyService;
    @Autowired
    private AirConditionHourlyService airConditionHourlyService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("gengyun:soilconditionhourly:list")
    public R list(@RequestParam Map<String, Object> params) {

        PageUtils page = soilConditionHourlyService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("gengyun:soilconditionhourly:info")
    public R info(@PathVariable("id") Long id) {
        SoilConditionHourlyEntity soilConditionHourly = soilConditionHourlyService.getById(id);

        return R.ok().put("soilConditionHourly", soilConditionHourly);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("gengyun:soilconditionhourly:save")
    public R save(@RequestBody SoilConditionHourlyEntity soilConditionHourly) {
        soilConditionHourlyService.save(soilConditionHourly);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("gengyun:soilconditionhourly:update")
    public R update(@RequestBody SoilConditionHourlyEntity soilConditionHourly) {
        soilConditionHourlyService.updateById(soilConditionHourly);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("gengyun:soilconditionhourly:delete")
    public R delete(@RequestBody Long[] ids) {
        soilConditionHourlyService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 根据某个地块来查询信息
     *
     * @param params
     * @return
     */
    @RequestMapping("/listById")
    @RequiresPermissions("gengyun:soilconditionhourly:list")
    public R listById(@RequestParam Map<String, Object> params) {

        int pageIndex = Integer.parseInt((String) params.get("page"));
        int pageSize = Integer.parseInt((String) params.get("limit"));
        String cropRotationId = (String) params.get("key");

        Page<SoilConditionHourlyEntity> page = new Page<>(pageIndex, pageSize);

        Page<SoilConditionHourlyEntity> list = soilConditionHourlyService.query()
                .eq("crop_rotation_id", cropRotationId)
                .page(page);
        return R.ok().put("page", new PageUtils(list));
    }

    /**
     * 根据地块 id , 开始时间与结束时间查询
     *
     * @param params
     * @return
     */
//
    @RequestMapping("/listByIdAndTime")
    @RequiresPermissions("gengyun:soilconditionhourly:list")
    public R listByIdAndTime(@RequestParam Map<String, Object> params) {
        String cropsRotationPlotId = (String) params.get("cropId");
        String date = (String) params.get("date");
        String time = (String) params.get("time");
        System.out.println(date+" "+time);
        cropsRotationPlotId = "12438";
        HashMap<String, String> submitTime = Date_Api.initHourlyData(date, time);

        String beginTime = submitTime.get("beginTime");
        String endTime = submitTime.get("endTime");

        List<Map> soilLOTList = soilConditionHourlyService.getSoilLOT(cropsRotationPlotId, beginTime, endTime);

        List<LOTEntity> LOTList = new ArrayList<>();

        String[] soilLOTTargets = {"土壤温度", "土壤湿度"};
        String[] soilLOTColumn = {"temperature_value", "humidity_value"};
        int soilLOTListLength = soilLOTList.size();
        int soilLOTTargetsLength = soilLOTTargets.length;
        for (int i = 0; i < soilLOTTargetsLength; i++) {
            LOTEntity lotEntity = new LOTEntity();
            lotEntity.setTargetName(soilLOTTargets[i]);
            int[] targetValue = new int[soilLOTListLength];
            for (int j = 0; j < soilLOTListLength; j++) {
                targetValue[j] = Integer.parseInt(soilLOTList.get(j).get(soilLOTColumn[i]).toString());
            }
            lotEntity.setTargetValue(targetValue);
            LOTList.add(lotEntity);
        }

        List<Map> airLOTList = airConditionHourlyService.getAirLOT(cropsRotationPlotId,beginTime,endTime);
        String[] airLOTTargets = {"空气湿度","降水量","空气温度"};
        String[] airLOTColumn = {"humidity_value","precipitation_value","temperature_value"};
        int airLOTListLength = airLOTList.size();
        int airLOTTargetsLength = airLOTTargets.length;
        for (int i = 0 ; i < airLOTTargetsLength; i ++ ){
            LOTEntity lotEntity = new LOTEntity();
            lotEntity.setTargetName(airLOTTargets[i]);
            int[] targetValue = new int[airLOTListLength];
            for (int j = 0; j < airLOTListLength; j++) {
                targetValue[j] = Integer.parseInt(airLOTList.get(j).get(airLOTColumn[i]).toString());
            }
            lotEntity.setTargetValue(targetValue);
            LOTList.add(lotEntity);
        }
//        for (LOTEntity lot : LOTList){
//            System.out.println(lot.getTargetValue().length);
//        }
//       List<Map<String,Object>> lotList = new Lis;

//        List<SoilConditionHourlyEntity> list = soilConditionHourlyService.query()
//                .eq("crop_rotation_id", cropRotationId)
//                .between("create_timestamp", beginTime, endTime)
//                .list();
//        // 处理时间
//        List<String> timestamps = new ArrayList<>();
//        // 处理温度
//        List<Double> temperatures = new ArrayList<>();
//        // 处理湿度
//        List<Double> humidities = new ArrayList<>();
//        list.forEach( x-> {
//            timestamps.add(x.getFormatTime());
//            temperatures.add(x.getTemperatureValue() / 100.0);
//            humidities.add(x.getHumidityValue() / 100.0);
//        });


//        return R.ok().put("timestamps", timestamps)
//                .put("temperatures", temperatures)
//                .put("humidities", humidities);
        return R.ok().put("getLOT", LOTList);
    }
}
